/* This is encsrv, a collection of initialization scripts to set up encryption for a web and database server, for GNU/Debian Linux Distributions based on systemd, particularly for Debian 8.
 * 
 * Copyright (C) 2016, gabocze
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * To contact the authors of encsrv visit https://gna.org/users/gabocze
 */

angular.module ('notes' ['ngCordova', 'xml'])
.config( function (x2jsProvider) {
  x2jsProvider.config ={arrayAccesFormPath: 'haus.note'};
}
)
.factory('format', function(x2js){
  return function(notes) {
    return x2js.json2xml_str ({haus: {note: notes}});
  }
})
.factory ('parse', function (x2js){
return function (xml) {
    return x2js.xml_str2json (xml).haus.note;
})
.factory('Notes', function($cordovaFile, format, x2js) {
  return {
    load: function () {
      // load from disk and return a list
         var deferred = $q.defer();
 document.addEventListener ('deviceready', function ( ) {
deferred.resolve($q.when ($cordovaFile.readFile (cordova.file.dataDirectory, 'notes.xml')));
} );  // addEventListener
  return deferred.promise .then (function (xml){
  return parse (xml);
}) ;
    },
    save: function (notes) {
  var deferred = $q.defer();
 document.addEventListener ('deviceready', function ( ) {
deferred.resolve($q.when ($cordovaFile.writeFile (cordova.file.dataDirectory, 'notes.xml', format (notes), false))); // resolve
} );  // addEventListener
  return deferred.promise;
    },
	newNote: function() {
		return {
			title: '',
			body: '',
			tags=''
		}
	}
  };  // Notes object
}); //Notes factory